<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\model;
use think\Model;

class SystemUser extends Model
{
	
    protected $name = 'system_user';  //表名
	protected $pk = 'user_id';  //主键		
	protected $fields = array(				//定义要处理的字段
		'user_guid', 'user_name',"user_pwd", 'user_code','user_phone','register_ip',
		'land_time', 'land_ip','land_status','is_admin', 'is_enable', 'only_no','org_id',
	); 
	
	//类型自动转换
	protected $type = [
        'land_time'  =>   'datetime',   //  '2015/5/1' ->  '2015-05-01 00:00:00'
        'create_time'  =>'datetime' //  '2015/5/1' ->  '2015-05-01 00:00:00'
    ];
	
	
	//自动完成
	protected $auto = [];
	// 自动完成, 只在新增的时候有效
	protected $insert = ['user_guid','register_ip','user_pwd'];
	// 自动完成, 只在更新的时候有效
	protected $update = ['land_time','land_ip'];
	protected function setUserPwdAttr($value){
        return md5('wei'.md5( request()->param("user_pwd") ?: 1 ));
    }
	protected function setUserGuidAttr() {
        return get_uuid();
    }
	protected function setRegisterIpAttr() {
        return request()->ip();
    }
	protected function setLandIpAttr() {
        return request()->ip();
    }
	protected function setLandTimeAttr() {
        return date("Y-m-d H:i:s");
    }
	
	
	/**
     * 修改密码
     * @param type $user_id
     * @param type $new_pwd
     * @return type
     */
	public function updatePwd($user_id,$new_pwd)
    {
		return $this ->where("user_id",$user_id)->update(['user_pwd' =>$new_pwd]);
    }
	
	/**
     * 新增用户
     * @param type $data
     * @return type
     */
	public function addUser($data)
    {
		return $this ->allowField(true) -> save($data);
    }
	
	/**
     * 启用或禁用
     * @param array $data 
     * @return type
     */
	public function updateData($data)
    {
		return $this->allowField(true)->saveAll($data);
    }
	
	/**
     * 根据组织查找用户
     * @param type $orgIds
     * @return type
     */
	public function selectOrgUser($orgIds)
    {
		return $this->where("org_id",'in',$orgIds)->select();
    }
	
}
